package com.le.tester.utils;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * createTime：2022/1/13 13:39
 * description：list处理工具类
 */
public class ListUtil {
    //将一个大list均分成n个list
    public static <T> List<List<T>> avgList(List<T> source, int n) {
        //是否能均分，取出余数
        List<List<T>> resultList = new ArrayList<>();
        int size = source.size();
        //取出余数
        int remainder = size % n;
        //商
        int number = size / n;
        //偏移量
        int offset = 0;

        for (int i = 0; i < n; i++) {
            List<T> value;
            if (remainder > 0) {
                //除不尽，进行偏移了
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                //别这样搞
//                remainder--;
//                offset++;
                remainder = new AtomicInteger(remainder).decrementAndGet();
                offset = new AtomicInteger(offset).incrementAndGet();
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            resultList.add(value);
        }
        return resultList;
    }

    //将一个大List切割成n个List(每个list中有count个元素)
    public static <T> List<List<T>> splitListWithFor(List<T> resList, int count) {
        if (CollectionUtils.isEmpty(resList) || count < 1) {
            return null;
        }
        List<List<T>> resultList = new ArrayList<>();
        int size = resList.size();
        if (size <= count) {
            resultList.add(resList);
        } else {
            //取得商
            int number = size / count;
            //取得余数
            int remainder = size % count;
            for (int i = 0; i < number; i++) {
                List<T> itemList = new ArrayList<>();
                for (int j = 0; j < count; j++) {
                    itemList.add(resList.get(i * count + j));
                }
                resultList.add(itemList);
            }
            //余数的处理
            if (remainder > 0) {
                List<T> itemList = new ArrayList<>();
                for (int i = 0; i < remainder; i++) {
                    itemList.add(resList.get(number * count + i));
                }
                resultList.add(itemList);
            }
        }
        return resultList;
    }

    //将一个大List-->Stream成n个List(效率不高)
    public static <T> List<List<T>> splitListWithStream(List<T> list, int count) {
        long t1 = System.currentTimeMillis();
        int limit = (list.size() + count - 1) / count;

        //获取分割后的集合，直接并行
        List<List<T>> result = Stream.iterate(0, n -> n + 1).limit(limit)
                .parallel()
                .map(a -> list.stream().skip(a * count).limit(count).parallel().collect(Collectors.toList()))
                .collect(Collectors.toList());
        long t2 = System.currentTimeMillis();
        System.out.println("duration is " + (t2 - t1));
        return result;
    }

//    public static void main(String[] args) {
//        List<Integer> list = new ArrayList<>();
//        for (int i = 0; i < 17; i++) {
//            list.add(i);
//        }
//        List<List<Integer>> avgList = avgList(list, 5);
//        System.out.println("avgList: " + avgList);
//
//		List<List<Integer>> splitList = splitListWithFor(list, 5);
//		System.out.println("splitList: " + splitList);
////
////		ArrayList<Integer> arr_list = new ArrayList<>();
////		for (int i = 0; i < splitList.size(); i++) {
////			List<Integer> subList = splitList.get(i);
////			arr_list.addAll(subList);
////		}
////		System.out.println("arr_list: " + arr_list);
//
//    }

}
